<%--
  Created by IntelliJ IDEA.
  User: 34123
  Date: 2023/6/30 0030
  Time: 下午 10:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link href="//unpkg.com/layui@2.8.8/dist/css/layui.css" rel="stylesheet">
    <!-- 引入 layui.js -->
    <script src="//unpkg.com/layui@2.8.8/dist/layui.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/jquery-3.6.0.js" charset="utf-8"></script>
</head>
<body>
<fieldset class="layui-elem-field layui-field-title">
    <legend>样板单编辑页面</legend>
</fieldset>

<div class="layui-form-item">

    <div class="layui-inline">
        <label class="layui-form-label">款号编号</label>
        <div class="layui-input-inline"><input type="text" name="itemNumber" lay-verify="required" placeholder="G\KG" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">款号类型</label>
        <div class="layui-input-inline"><input type="text" name="itemNumber" lay-verify="required" placeholder="XX款式单位" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">款号样式</label>
        <div class="layui-input-inline"><input type="text" name="itemNumber" lay-verify="required" placeholder="其他1" class="layui-input">
        </div>
    </div>
    <button type="button" class="layui-btn" style="right: 200px" >确定修改</button>
    <button type="button" class="layui-btn" style="right: 200px" onclick="toAddStyleNumber()">添加款号</button>
    <table class="layui-hide" id="ID-table-demo-data1" lay-filter="test"></table>
</div>
</body>
<script src="//unpkg.com/layui@2.8.8/dist/layui.js"></script>
<script>
    layui.use('table', function(){
        var table = layui.table;

        // 已知数据渲染
        var inst = table.render({
            elem: '#ID-table-demo-data1',
            url: 'SampleSheet/getStyleNumberList',//数据接口
            cols: [[ //标题栏
                {field: 'styleNumberId', title: 'ID', width: 80, sort: true},
                {field: 'styleNumber', title: '款号', width: 200},
                {field: 'brand', title: '品牌', width: 160},
                {field: 'styleName', title: '款式名称', width: 160},
                {field: '', title: '操作', width: 180,align: 'center', /*toolbar: '#templet-demo-theads-tool',*/
                    templet: function (d) {
                        // alert(d.auditStatus )
                        if (d.createState == "1") {
                            return '<button class="layui-btn layui-btn-danger layui-btn-sm layui-btn-disabled" id="barDemo"  >已创建样板单</button>' +
                                '<button type="button" class="layui-btn layui-btn-danger layui-btn-sm layui-btn-disabled" lay-on="alert" style="background-color: #ff5722; color: white">删除</button>\n';
                        } else if (d.createState == "0") {
                            return '\n' +
                                '        <button type="button"  lay-event="toCreateSampleSheet" class="layui-btn layui-btn-primary layui-btn-sm " style="background-color: #1e9fff; color: white" >创建样板单</button>\n' +
                                '        <button type="button" class="layui-btn layui-btn-primary layui-btn-sm " lay-event="deleteProduct" lay-on="alert" style="background-color: #ff5722; color: white">删除</button>\n' +
                                '   ';
                        }
                    }
                },
            ]],
            page: true, // 是否显示分页
            limits: [5, 10, 15],
            limit: 5 // 每页默认显示的数量
        });
    });

</script>
<script type="text/html" id="templet-demo-theads-tool">
    <div class="layui-btn-group">
        <button type="button"  lay-event="toCreateSampleSheet" class="layui-btn layui-btn-primary layui-btn-sm " style="background-color: #1e9fff; color: white" >创建样板单</button>
        <button type="button" class="layui-btn layui-btn-primary layui-btn-sm " lay-on="alert" style="background-color: #ff5722; color: white">删除</button>
    </div>
</script>
<script type="text/javascript">
    function toAddStyleNumber() {
       // window.location.href="toAddStyleNumber";
        layer.open({
            type: 1, // page 层类型
            area: ['800px', '500px'],
            title: '添加款号',
            shade: 0.6, // 遮罩透明度
            shadeClose: true, // 点击遮罩区域，关闭弹层
            maxmin: true, // 允许全屏最小化
            anim: 0, // 0-6 的动画形式，-1 不开启
            content: '<form>  <div class="layui-form-item">\n' +
                '    <div class="layui-inline">\n' +
                '        <label class="layui-form-label">款号</label>\n' +
                '        <div class="layui-input-inline"><input type="text" name="styleNumber" lay-verify="required"\n' +
                '                                               class="layui-input layui-disabled" value="款号自动生成" readonly></div>\n' +
                '    </div>\n' +
                '    <div class="layui-inline">\n' +
                '        <label class="layui-form-label">款号品牌</label>\n' +
                '        <div class="layui-input-inline"><input type="text" name="brand" lay-verify="required" class="layui-input " placeholder="款号品牌" ></div>\n' +
                '    </div>\n' +
                '    <div class="layui-inline">\n' +
                '        <label class="layui-form-label">款式名称</label>\n' +
                '        <div class="layui-input-inline"><input type="text" name="styleName" lay-verify="required" class="layui-input " placeholder="款式名称" ></div>\n' +
                '    </div>\n' +
                '    <div class="layui-inline">\n' +
                '        <div class="layui-input-inline"><input type="hidden" name="logicDel" lay-verify="required" class="layui-input "  ></div>\n' +
                '        <div class="layui-input-inline"><input type="hidden" name="state" lay-verify="required" class="layui-input "  ></div>\n' +
                '        <div class="layui-input-inline"><input type="hidden" name="styleNumberImg" lay-verify="required" class="layui-input "  ></div>\n' +
                '    </div>\n' +
                '    <fieldset class="layui-elem-field layui-field-title">\n' +
                '        <legend>文件上传</legend>\n' +
                '    </fieldset>\n' +
                '    <hr style="margin: 21px 0;">\n' +
                '    <div class="layui-upload">\n' +
                '        <button type="button" class="layui-btn layui-btn-normal" id="ID-upload-demo-files">选择多文件</button>\n' +
                '        <div class="layui-upload-list">\n' +
                '            <table class="layui-table">\n' +
                '                <colgroup>\n' +
                '                    <col style="min-width: 100px;">\n' +
                '                    <col width="150">\n' +
                '                    <col width="260">\n' +
                '                    <col width="150">\n' +
                '                </colgroup>\n' +
                '                <thead>\n' +
                '                <th>文件名</th>\n' +
                '                <th>大小</th>\n' +
                '                <th>上传进度</th>\n' +
                '                <th>操作</th>\n' +
                '                </thead>\n' +
                '                <tbody id="ID-upload-demo-files-list"></tbody>\n' +
                '            </table>\n' +
                '        </div>\n' +
                '        <button type="button" class="layui-btn" id="ID-upload-demo-files-action">开始上传</button>\n' +
                '    </div>\n' +
                '<button type="button" class="layui-btn layui-btn-primary" style="float: right;margin-right: 300px" onclick="toBack()">取消</button>\n' +
                '    <button type="button" class="layui-btn" style="float: right;margin-right: 10px" lay-event="handleImageUpload" onclick="addStyleNumber()">添加款号</button>\n' +
                '</div>\n' +
                '</form>'
        });
        layui.use(function(){
            var upload = layui.upload;
            var element = layui.element;
            var $ = layui.$;
            // 制作多文件上传表格
            var uploadListIns = upload.render({
                elem: '#ID-upload-demo-files',
                elemList: $('#ID-upload-demo-files-list'), // 列表元素对象
                url: 'SampleSheet/addStyleNumber', // 此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
                accept: 'file',
                multiple: true,
                number: 3,
                auto: false,
                bindAction: '#ID-upload-demo-files-action',
                choose: function(obj){
                    var that = this;
                    var files = this.files = obj.pushFile(); // 将每次选择的文件追加到文件队列
                    // 读取本地文件
                    obj.preview(function(index, file, result){
                        var tr = $(['<tr id="upload-'+ index +'">',
                            '<td>'+ file.name +'</td>',
                            '<td>'+ (file.size/1024).toFixed(1) +'kb</td>',
                            '<td><div class="layui-progress" lay-filter="progress-demo-'+ index +'"><div class="layui-progress-bar" lay-percent=""></div></div></td>',
                            '<td>',
                            '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>',
                            '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>',
                            '</td>',
                            '</tr>'].join(''));

                        // 单个重传
                        tr.find('.demo-reload').on('click', function(){
                            obj.upload(index, file);
                        });

                        // 删除
                        tr.find('.demo-delete').on('click', function(){
                            delete files[index]; // 删除对应的文件
                            tr.remove(); // 删除表格行
                            // 清空 input file 值，以免删除后出现同名文件不可选
                            uploadListIns.config.elem.next()[0].value = '';
                        });

                        that.elemList.append(tr);
                        element.render('progress'); // 渲染新加的进度条组件
                    });
                },
                done: function(res, index, upload){ // 成功的回调
                    var that = this;
                    // if(res.code == 0){ // 上传成功
                    var tr = that.elemList.find('tr#upload-'+ index)
                    var tds = tr.children();
                    tds.eq(3).html(''); // 清空操作
                    delete this.files[index]; // 删除文件队列已经上传成功的文件
                    return;
                    //}
                    this.error(index, upload);
                },
                allDone: function(obj){ // 多文件上传完毕后的状态回调
                    console.log(obj)
                },
                error: function(index, upload){ // 错误回调
                    var that = this;
                    var tr = that.elemList.find('tr#upload-'+ index);
                    var tds = tr.children();
                    // 显示重传
                    tds.eq(3).find('.demo-reload').removeClass('layui-hide');
                },
                progress: function(n, elem, e, index){ // 注意：index 参数为 layui 2.6.6 新增
                    element.progress('progress-demo-'+ index, n + '%'); // 执行进度条。n 即为返回的进度百分比
                }
            });
        });
        var upload = layui.upload; //得到 upload 对象1
        upload.render({//创建一个上传组件
            elem: '.test'
            ,done: function(res, index, upload){
                var item = this.item;
            }
        })

    }
</script>

<script>
    layui.use(['table', 'form', 'laydate'], function() {
        var table = layui.table;
        table.on('tool(test)', function(obj){
            if(obj.event === 'toCreateSampleSheet'){
                let styleNumberId=obj.data.styleNumberId;
                let styleNumber=obj.data.styleNumber;
                window.location.href="toCreateSampleSheet?styleNumberId="+styleNumberId+"&styleNumber="+styleNumber;
            }else if (obj.event === 'deleteProduct'){
                let id=obj.data.styleNumberId;
                //layer.alert(id)
                $.ajax({
                    url: 'SampleSheet/toDelStyleNumById',
                    type: 'post',
                    data: {"styleNumberId":id},
                    dataType: 'json',
                    success: function(Result) {
                        let msg=Result.message;
                        if (Result.code === 200) {
                            layer.alert(msg);
                            table.reload('demo')
                        }else {
                            layer.alert(msg);
                        }
                        table.render();
                    },
                    error:function () {
                        layer.alert('网络正忙！');
                    }
                });
            }
        });
    });

</script>
<script src="//unpkg.com/layui@2.8.8/dist/layui.js"></script>
<script>

</script>
<script type="text/javascript">
function toBack() {
    window.location.href="toStyleNumberList";
}

//base64图片转码
/*function handleImageUpload(event) {
    var file = event.target.files[0];
    var reader = new FileReader();

    reader.onload = function(e) {
        var imageElement = document.getElementById("files");
        imageElement.src = e.target.result;

        var base64String = e.target.result.split(",")[1];
        console.log("Base64 Encoded Image:", base64String);
    };

    reader.readAsDataURL(file);
}*/
function addStyleNumber() {
/*

    let randomNumber=Math.floor(Math.random()*(999999-100000+1))+100000;//生成100000到999999的随机数
    let styleNumber1='KH'+randomNumber;
    //alert(styleNumber1)
    $("input[name='styleNumber']").val(styleNumber1);
*/

    /*var files = $('#ID-upload-demo-files')[0].files;
    for (let j = 0; j < files.length; j++) {
        let file = files[j];
        data={'files':file};
    }*/
    let data ={};
    let value = $('form').serializeArray();
    $.each(value, function (index,item) {
        data[item.name] = item.value;
    });
    let styleNumber = JSON.stringify(data);
    console.log(styleNumber);
    $.ajax({
        url: 'SampleSheet/addStyleNumber',
        type: 'post',
        data:styleNumber,
        dataType: 'json',
        "contentType":"application/json;charset=utf-8;",
        success: function(Result) {
            if (Result.code === 200) {
                layer.alert('添加成功！'+'款号为：'+Result.data);
                table.reload('demo')
            }else {
                layer.alert('添加失败或已经添加！');
            }
            table.render();
        },
        error:function () {
            layer.alert('网络正忙！');
        }
    });
}
</script>
</html>
